﻿@using GMS.NET.Dto.System.Menus.Shared
@inject GMS.NET.Web.Components.Service.Interface.System.IMenusService MenuService
@inject MessageAlert MsgAlert
@inherits ComponentBase

<Form>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_Name" IsTrim="true" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_Icon" IsDisabled="disResIcon" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_CssClass" IsTrim="true" />
    </FormItem>
    <FormItem>
        <AutoComplete Items="@Items" ValueChanged="@ResParentIdOnValueChanged"
                      Debounce="1000" OnSelectedItemChanged="ResParentIdOnChanged" DisplayText="父级资源" />
    </FormItem>
    <FormItem>
        <Select @bind-Value="context.Res_Type" OnSelectedItemChanged="ResTypeOnChanged" />
    </FormItem>
    <FormItem>
        <Select @bind-Value="context.Res_Disabled" />
    </FormItem>
    <FormItem>
        <BootstrapInputNumber @bind-Value="context.Res_Sort" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_Url" IsTrim="true" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_CodeName" IsTrim="true"
                        PlaceHolder="需要代码生成请填" IsDisabled="@(context.Id!=Guid.Empty)" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_CsFileName" IsTrim="true" IsDisabled="true"
                        PlaceHolder="选择父级资源后自动获取" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@context.Res_CsFileCatalog" IsTrim="true"
                        PlaceHolder="需要代码生成请填" IsDisabled="@(context.Res_Type!=Dto.System.Menus.Enums.Res_Type.sysmenu
                        || context.Id!=Guid.Empty)" />
    </FormItem>
    <FormItem>
        <Switch OnColor="Color.Success" @bind-Value="@context.IsOpenCodeCreate" OnText="开启"
                OffText="关闭" IsDisabled="@(context.Id!=Guid.Empty)" OnValueChanged="OpenCodeChange" />
    </FormItem>
    <FormItem Cols="12">
        <Textarea @bind-Value="@context.Res_Remark" rows="3" />
    </FormItem>
</Form>

@code{
    [Parameter]
    public MenusTree context { get; set; }
    private readonly List<string> _items = new();
    private IEnumerable<string> Items => _items;
    private List<KeyValues> FilterDatas = new();
    private async Task ResParentIdOnValueChanged(string val)
    {
        _items.Clear();
        FilterDatas = await MenuService.GetParentList(val);
        foreach (var item in FilterDatas)
        {
            _items.Add(item.Key!);
        }
    }
    private async Task ResParentIdOnChanged(string val)
    {
        context.Res_ParentId = new Guid(FilterDatas.First(x => x.Key == val).Value!.ToString()!);
        var result = await MenuService.GetCodeInfo(context.Res_ParentId);
        context.Res_CsFileName = result.Res_CsFileName;
        context.Res_CsFileCatalog = result.Res_CsFileCatalog;
        StateHasChanged();
    }

    protected override Task OnInitializedAsync()
    {
        return base.OnInitializedAsync();
    }
    bool disResIcon = false;
    private Task ResTypeOnChanged(SelectedItem Item)
    {
        disResIcon = (Item.Value == "apiInterface" || Item.Value == "field");
        context.Res_Icon = Item.Value == "apiInterface" ? "fas fa-link" : Item.Value == "field" ? "fas fa-table-columns" : context.Res_Icon;
        return Task.CompletedTask;
    }
    private async Task OpenCodeChange(bool val)
    {
        if (val)
            await MsgAlert.Info("请注意代码生成功能仅可在开发阶段使用,请勿在生产环境开启.后续持续更新更强大的代码生成配置,敬请期待.");
    }
    //private Task ResTypeOnChanged(SelectedItem item)
    //{
    //    var enums = (Res_Type)Enum.Parse(typeof(Res_Type), item.Value);
    //    switch (enums)
    //    {
    //        case Res_Type.sysdirectory:
    //            context.Res_Url = "no-address";
    //            context.Res_ParentId = new Guid(DataConst.ParentGuid);
    //            break;
    //        case Res_Type.sysmenu:
    //            context.Res_Url = "";
    //            break;
    //        case Res_Type.apiInterface:
    //            context.Res_Url = "no-address";
    //            break;
    //        case Res_Type.field:
    //            context.Res_Url = "no-address";
    //            break;
    //        default:
    //            break;
    //    }
    //    return Task.CompletedTask;
    //}
}